﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iReaper.IndexBuilder.XmlSource;
using iReaper.IndexBuilder.WWESource;
using System.Xml.Serialization;
using System.Collections;
using System.IO;
using System.Xml;

namespace iReaper.IndexBuilder
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ChannelInfo[]));

            WWESource.WWERootParser parser = new iReaper.IndexBuilder.WWESource.WWERootParser();
            Console.WriteLine("Loading channels...");
            ChannelInfo[] info = parser.GetChannelInfo();

            Console.WriteLine("Loading category...");
            WWECategoryCrawler wweCrawler = new WWECategoryCrawler();
            wweCrawler.Process(info);

            Console.WriteLine("Loading detail...");
            WWEDetailCrawler detailCrawler = new WWEDetailCrawler();
            detailCrawler.Process(info);

            Console.WriteLine("Checking landing url");
            LiveMeetingLandingUrlChecker checker = new LiveMeetingLandingUrlChecker();
            checker.Process(info);

            StreamWriter fileWriter = new StreamWriter("C:\\temp\\result.xml", false, Encoding.UTF8);
            serializer.Serialize(fileWriter, info);
            fileWriter.Close();

            return;
        }

        static void BuildInfoXml(string src, string output)
        {

            XmlTextWriter writer = new XmlTextWriter(output, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartElement("Webcasts");

            XmlDocument xml = new XmlDocument();
            xml.Load(src);

            XmlNodeList eventList = xml.SelectNodes("//Event");
            int i = 0;
            foreach (XmlNode nEvent in eventList)
            {
                if (nEvent.SelectSingleNode("LiveMeetingUrl") == null)
                {
                    continue;
                }

                DateTime date = DateTime.Parse(nEvent.SelectSingleNode("Publish").InnerText);
                string download = nEvent.SelectSingleNode("LiveMeetingUrl").InnerText;
                string url = nEvent.SelectSingleNode("DetailUrl").InnerText;
                string title = nEvent.SelectSingleNode("Title").InnerText;
                string description = nEvent.SelectSingleNode("Description").InnerText;
                string category = nEvent.SelectSingleNode("../../Category").InnerText;

                XmlNode nAuthor = nEvent.SelectSingleNode(".//Author");
                string author = null, authorTitle = null, resume = null;
                if (nAuthor != null)
                {
                    author = nAuthor.SelectSingleNode("Name").InnerText;

                    XmlNode nTitle = nAuthor.SelectSingleNode("Title");
                    authorTitle = nTitle == null ? null : nTitle.InnerText;

                    XmlNode nResume = nAuthor.SelectSingleNode("Introduce");
                    resume = nResume == null ? null : nResume.InnerText;
                }
                writer.WriteStartElement("R");

                writer.WriteElementString("newsID", i.ToString());
                writer.WriteElementString("headline", title);
                writer.WriteElementString("description", description);
                writer.WriteElementString("url", url);
                // level
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "level");
                writer.WriteAttributeString("attValue", "200");
                writer.WriteEndElement();

                // speaker
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "speaker");
                writer.WriteAttributeString("attValue", author);
                writer.WriteEndElement();

                // title
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "title");
                writer.WriteAttributeString("attValue", authorTitle);
                writer.WriteEndElement();

                // resume
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "resume");
                writer.WriteAttributeString("attValue", resume);
                writer.WriteEndElement();

                // tech
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "tech");
                writer.WriteAttributeString("attValue", category);
                writer.WriteEndElement();

                // tech
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "video");
                writer.WriteAttributeString("attValue", download);
                writer.WriteEndElement();

                // date
                writer.WriteStartElement("a");
                writer.WriteAttributeString("attCatID", "date");
                writer.WriteAttributeString("attValue", date.ToString());
                writer.WriteEndElement();


                writer.WriteEndElement();
                i++;
            }

            writer.WriteEndElement();
            writer.Close();
        }
    }
}
